<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<#include "/admin/include/head.htm">
<script type="text/javascript" src="${base}/resource/js/TreeSelector.js"></script>
<script type="text/javascript">
var goodsCategoryTree = null;
var selectCategory = null;
var leafOnly = false;
$().ready(function() {
	
	var $inputForm = $("#inputForm");
	var $buildType = $("#buildType");
	var $articleCategoryTr = $("#articleCategoryTr");
	var $articleCategoryId = $("#article_category");
	var $productCategoryTr = $("#productCategoryTr");
	var $goodsCategory = $("#goodsCategoryName");
	var $goodsCategoryId = $("#goodsCategoryId");
	var $beginDateTr = $("#beginDateTr");
	var $beginDate = $("#beginDate");
	var $endDateTr = $("#endDateTr");
	var $endDate = $("#endDate");
	var $submit = $(":submit");
	
	var buildCount;
	var buildTime;
	var buildType;
	var articleCategoryId;
	var goodsCategoryId;
	var goods_category_id;
	var beginDate;
	var endDate;
	
	$buildType.change(function() {
		var $this = $(this);
		if ($this.val() == "article") {
			$articleCategoryTr.show();
			$productCategoryTr.hide();
			$beginDateTr.show();
			$endDateTr.show();
		} else if ($this.val() == "goods") {
			$articleCategoryTr.hide();
			$productCategoryTr.show();
			$beginDateTr.show();
			$endDateTr.show();
		} else {
			$articleCategoryTr.hide();
			$productCategoryTr.hide();
			$beginDateTr.hide();
			$endDateTr.hide();
		}
	});
	
	$("#goodsCategoryName").focus(function() {
		if(goodsCategoryTree) {
			goodsCategoryTree.show();
		} else {
			goodsCategoryTree = $.ligerDialog.open({
				url: '${base}/admin/b2c/goods/goodsCategoryTree.jhtml',
				left: $(this).offset().left,
				top: $(this).offset().top,
				height: 400,
				width: null,
				modal: true,
				titie: '商品分类', 
				buttons: [{
					text: '确定',
					onclick: function(item, dialog) {
						if(selectCategory) {
							selectCategory();
						}
					}
				}, { 
					text: '取消', 
					onclick: function (item, dialog) {
						dialog.hide(); 
					} 
				}] 
			});
		}
	});
	
    // 验证
	$inputForm.validate({
		rules: {
		},
		submitHandler: function(form) {
			buildCount = 0;
			buildTime = 0;
			buildType = $buildType.val();
			articleCategoryId = $articleCategoryId.val();
			goodsCategory = $goodsCategory.val();
			goodsCategoryId = $goodsCategoryId.val();
			if(!goodsCategoryId || goodsCategory == '' || goodsCategory == '搜索商品') {
				goodsCategoryId = '';
			}
			beginDate = $beginDate.val();
			endDate = $endDate.val();
			$buildType.attr("disabled", true);
			$articleCategoryId.attr("disabled", true);
			$goodsCategory.attr("disabled", true);
			$goodsCategoryId.attr("disabled", true);
			$beginDate.attr("disabled", true);
			$endDate.attr("disabled", true);
			$submit.attr("disabled", true);
			build();
		}
	});	
	
	function build() {
		$.ajax({
			url: base + '/admin/b2c/lucene/buildIndex.jhtml',
			type: "POST",
			data: {
				buildType: buildType, 
				goodsCategoryId: goodsCategoryId, 
				beginDate: beginDate, 
				endDate: endDate
			},
			dataType: "json",
			success: function(data) {
                if (data.isCompleted) {
                    $buildType.attr("disabled", false);
                    $articleCategoryId.attr("disabled", false);
                    $goodsCategory.attr("disabled", false);
                    $goodsCategoryId.attr("disabled", false);
                    $beginDate.attr("disabled", false);
                    $endDate.attr("disabled", false);
                    $submit.attr("disabled", false);
                }

				if(data.buildCount == -1){
                    LG.showSuccess("异步执行成功");
				}else{
                    buildCount += data.buildCount;
                    buildTime += data.buildTime;
                    var time;
                    if (buildTime < 60000) {
                        time = (buildTime / 1000).toFixed(2) + "秒";
                    } else {
                        time = (buildTime / 60000).toFixed(2) + "分";
                    }
                    LG.showSuccess("生成静态成功 ,生成数: " + buildCount + "耗时:" + time + "]");
				}
			},
			error: function (message) {
			  LG.showError(message.response);
			}
		});
	}
});
</script>
</head>
<body>
<form action=''  method="post" id="inputForm">
<div id="tabcontainer" style="overflow:hidden;margin:3px;">
	<table class="table">
		<tr>
			<th>生成类型：</th>
			<td>
				<select id="buildType" name="buildType">
					<option value="goods" selected="selected">商品</option>
				</select>
			</td>
		</tr>
		<tr id="productCategoryTr" >
			<th>商品分类：</th>
			<td>
				<input type="text" id="goodsCategoryName" value="${(goods.goodsCategory.name)!'全部分类'}"	
					class="formText"/>										
				<input type="hidden" id="goodsCategoryId" name="goodsCategoryId" value="${(goods.goodsCategory.id)!}"/>
			</td>
		</tr>
		<tr id="articleCategoryTr" class="hidden">
			<th>文章分类：</th>
			<td><select id="article_category" name="articleCategoryId"></select></td>
		</tr>
		<tr id="beginDateTr" >
			<th>开始时间：</th> 
			<td><input type="text" name="beginDate" id="beginDate" value="${defaultBeginDate?string("yyyy-MM-dd")!''}" 
					class="formText {required: true}" onfocus="WdatePicker({maxDate: '#F{$dp.$D(\'endDate\')}'});"/>
			</td>
		</tr>
		<tr id="endDateTr">
			<th>结束时间：</th> 
			<td><input type="text" name="endDate" id="endDate" value="${defaultEndDate?string("yyyy-MM-dd")!''}" 
					class="formText {required: true}" onfocus="WdatePicker({maxDate: '#F{$dp.$D(\'endDate\')}'});"/>
			</td>
		</tr>
		<tr>
			<th>
				&nbsp;
			</th>
			<td>
				<input type="submit" class="button" value="提交" />
			</td>
		</tr>
	</table>
</div>
</form>
</body>
</html>